<?php
//exit();
set_time_limit(0);
$_isDBExist = "no";
$_DBSite = "Master";
global $_Conn124;
global $_ConnMaster;
$_BetDBHost = "192.168.5.124";
$_BetDBUser = "kacino";
$_BetDBPass = "cyriac_0426";
if (! $_Conn124 = @mysql_connect($_BetDBHost, $_BetDBUser, $_BetDBPass)) {
	$email = "service@".$_BetWeb;
	$subject = " mysql_connect error (".$_BetWeb.")";
	$content = "連線錯誤：無法連線至資料庫主機！\nDBSite".$_DBSite."\nPage: ".$_SERVER["PHP_SELF"]."\n";
	if($_isDBExist == "no"){
		$_strLogErr = $content."<br />\n";
		mail($email, $subject, $content, "From:".$_BetWeb."<apache@secure.".$_BetWeb.">");
		exit($_strLogErr);
	}
}
mysql_query("SET NAMES 'utf8'", $_Conn124);

$_Conn = "_ConnMaster";
$strDate = date("Y-m-d");
$strYesterday = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d") - 1, date("Y")));
include("AllWeb.inc.php");

$strMailCon = "";
if (!defined('__Modules_Path')) define('__Modules_Path', '/home/share/www/SourceSite-utf8/secure/modules');
foreach($arrWeb as $strWeb){
	$_strLogErr = "";
	$_blDBName = "";
	$_blDBHostM = "";
	$_ConnMaster = null;
	if($strWeb == "luckyball.cc")continue;
	//if($strWeb != "bb.usbet777.us")continue;
	$strMailCon .= "\n\n".$strWeb."<br />\n==============================================<br />\n";

	$_SERVER["DOCUMENT_ROOT"] = "/home/share/www/".$strWeb."/secure";
	if(!file_exists($_SERVER["DOCUMENT_ROOT"]."/db.inc.php"))continue;
	include($_SERVER["DOCUMENT_ROOT"]."/db.inc.php");
	if($_strLogErr != ""){
		$strMailCon .= $_strLogErr."<br />\n\n";
		continue;
	}
	if($_blDBName == "")continue;
	$strMailCon .= "$_blDBName  $_blDBHostM  (Time: ".date("Y-m-d H:i:s").")<br />\n\n";

	if(!mysql_select_db($_blDBName, $_Conn124)){
		$strMailCon .= "LogRecord Error :".mysql_error($_Conn124)."<br />\n";
		continue;
	}

	$arrSdSQL[1] = "select `ID`, `LottoID`, `LType`, `Bet_Type`, `GambleID`, `Number`, `OddsID`, `OddsID2`, `Odds`, `Odds2`, `MemID`, `MemExchangeRate`, `MemCurrency`, `MemStake`, `MemPayOff`, `MemResult`, `MemCommission`, `SubID`, `SubIncome`, `SubCommission`, `SubResult`, `SubRate`, `SubRateResult`, `MainID`, `MainCommission`, `MainResult`, `MainRate`, `MainRateResult`, `StockID`, `StockCommission`, `StockResult`, `StockRate`, `StockRateResult`, `DirectorID`, `DirectorCommission`, `DirectorResult`, `DirectorRate`, `DirectorRateResult`, `PayupDate`, `isPayOff`, `WagerType` from `bet_BetList` where ID > ";

	$arrInSQL[1] = "insert into `bet_BetList` (`ID`, `LottoID`, `LType`, `Bet_Type`, `GambleID`, `Number`, `OddsID`, `OddsID2`, `Odds`, `Odds2`, `MemID`, `MemExchangeRate`, `MemCurrency`, `MemStake`, `MemPayOff`, `MemResult`, `MemCommission`, `SubID`, `SubIncome`, `SubCommission`, `SubResult`, `SubRate`, `SubRateResult`, `MainID`, `MainCommission`, `MainResult`, `MainRate`, `MainRateResult`, `StockID`, `StockCommission`, `StockResult`, `StockRate`, `StockRateResult`, `DirectorID`, `DirectorCommission`, `DirectorResult`, `DirectorRate`, `DirectorRateResult`, `PayupDate`, `isPayOff`, `WagerType`) values  ";

	$arrMaxSQL[1] = "select MAX(ID) as MaxID from `bet_BetList`";

	$arrCheckSQL[1] = "select SUM(`MemStake`) + SUM(`MainResult`) as CheckValue from `bet_BetList` where ID > %d and ID <= %d";


	$arrSdSQL[2] = "select `ID`, `GameDate`, `BetDate`, `BetTime`, `Content`, `checkkey`, `GambleType`, `MemAccount`, `isBlack`, `NNWin`, `NSWin`, `JoinNumber`, `OneStake`, `SuppleID` from `bet_BetListContent` where ID > ";

	$arrInSQL[2] = "insert into `bet_BetListContent` (`ID`, `GameDate`, `BetDate`, `BetTime`, `Content`, `checkkey`, `GambleType`, `MemAccount`, `isBlack`, `NNWin`, `NSWin`, `JoinNumber`, `OneStake`, `SuppleID`) values  ";

	$arrMaxSQL[2] = "select MAX(ID) as MaxID from `bet_BetListContent`";

	$arrCheckSQL[2] = "select SUM(`GambleType`) + SUM(`SuppleID`) as CheckValue from `bet_BetListContent` where ID > %d and ID <= %d";


	$arrSdSQL[3] = "select `ID`, `Account`, `Password`, `Name`, `MemberGroup`, `ParentID`, `SubID`, `MainID`, `StockID`, `DirectorID`, `AddDate`, `AddTime` from `bet_Member` where `MemberGroup` not in (11, 12, 13, 14, 15) and ID > ";

	$arrInSQL[3] = "insert into `bet_Member`(`ID`, `Account`, `Password`, `Name`, `MemberGroup`, `ParentID`, `SubID`, `MainID`, `StockID`, `DirectorID`, `AddDate`, `AddTime`) values ";

	$arrMaxSQL[3] = "select MAX(ID) as MaxID from `bet_Member`";

	$arrCheckSQL[3] = "select SUM(`MemberGroup`) + SUM(`ParentID`) as CheckValue from `bet_Member` where MemberGroup not in (11,12,13,14,15) and ID > %d and ID <= %d";


	$strSQL = "
				select MAX(ID) as ID, '1' as TT from	`bet_BetList`
				union
				select MAX(ID) as ID, '2' as TT from	`bet_BetListContent`
				union
				select MAX(ID) as ID, '3' as TT from	`bet_Member`			
	";

	$arrMaxID = array();
	if(!$RS = PMA_mysql_query($strSQL, $_Conn124)){
		$strMailCon .= "Member Find Error :".PMA_mysql_error($_Conn124)."<br />\n";
	}else{
		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrMaxID[$Rows -> TT] = (int)$Rows -> ID;
		}
	}
	foreach($arrMaxID as $intIndex => $intID){
		$strSQL = $arrSdSQL[$intIndex].$intID;
		if(!$RS = PMA_mysql_query($strSQL, $_ConnMaster)){
			$strMailCon .= "Find Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
			continue;
		}

		$arrRow = array();
		$i = 0;
		while($Rows = PMA_mysql_fetch_array($RS, MYSQL_ASSOC)){
			foreach($Rows as $k => $v){
				$Rows[$k] = addslashes($v);
			}
			$arrRow[floor($i / 2000)][] = "('".join("', '", $Rows)."')";
			$i++;
		} //while($Rows = PMA_mysql_fetch_array($RS, MYSQL_ASSOC))

		$intAffectRows = 0;
		if(sizeof($arrRow) > 0){
			foreach($arrRow as $arrTmpRow){
				$strSQL = $arrInSQL[$intIndex].join(", ", $arrTmpRow);
				if(!PMA_mysql_query($strSQL, $_Conn124)){
					$strMailCon .= $intIndex." Error :".PMA_mysql_error($_Conn124)."<br />\n";
					continue;
				}
				$intAffectRows += mysql_affected_rows($_Conn124);
			}

			$strSQL = $arrMaxSQL[$intIndex];
			if(!$RS = PMA_mysql_query($strSQL, $_Conn124)){
				$strMailCon .= "Find Error :".PMA_mysql_error($_Conn124)."<br />\n";
				continue;
			}
			$Rows = PMA_mysql_fetch_object($RS);
			$intMaxID = $Rows -> MaxID;

			$strSQL = sprintf($arrCheckSQL[$intIndex], $intID, $intMaxID);
			if(!$RS = PMA_mysql_query($strSQL, $_Conn124)){
				$strMailCon .= "Find Error :".PMA_mysql_error($_Conn124)."<br />\n";
				continue;
			}
			$Rows = PMA_mysql_fetch_object($RS);
			$intNewCheckValue = floor($Rows -> CheckValue);

			if(!$RS = PMA_mysql_query($strSQL, $_ConnMaster)){
				$strMailCon .= "Find Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
				continue;
			}
			$Rows = PMA_mysql_fetch_object($RS);
			$intOldCheckValue = floor($Rows -> CheckValue);

			if($intOldCheckValue == $intNewCheckValue){
				$strMailCon .= $intIndex." OK(".$intAffectRows.") (Min:".$intID.", Max:".$intMaxID.")<br />\n";
			}else{
				$strMailCon .= $intIndex." Error(".$intAffectRows.") (Old:".$intOldCheckValue.", New:".$intNewCheckValue.")<br />\n";
				$strMailCon .= "SQL: ".$strSQL."<br />\n";
			}
		} //if(sizeof($arrRow) > 0)
	} //foreach($arrMaxID as $intIndex => $intID)

	//======================================================
	//因七星彩有跨日期經營，所以之前轉移資料可能有未結帳或註銷的
	$strSQL = $arrSdSQL[1]." 0 and `PayupDate` = '".$strYesterday."' ";
	if(!$RS = PMA_mysql_query($strSQL, $_ConnMaster)){
		$strMailCon .= "Find Error :".PMA_mysql_error($_ConnMaster)."<br />\n";
		continue;
	}

	$arrRow = array();
	$i = 0;
	while($Rows = PMA_mysql_fetch_array($RS, MYSQL_ASSOC)){
		foreach($Rows as $k => $v){
			$Rows[$k] = addslashes($v);
		}
		$arrRow[floor($i / 2000)][] = "('".join("', '", $Rows)."')";
		$i++;
	} 

	$intAffectRows = 0;
	if(sizeof($arrRow) > 0){
		foreach($arrRow as $arrTmpRow){
			$strSQL = $arrInSQL[1].join(", ", $arrTmpRow)." ON DUPLICATE KEY UPDATE `isPayOff` = VALUES(isPayOff), `MemResult` = VALUES(MemResult), `SubIncome` = VALUES(SubIncome), `SubResult` = VALUES(SubResult), `SubRateResult` = VALUES(SubRateResult), `MainResult` = VALUES(MainResult), `MainRateResult` = VALUES(MainRateResult), `StockResult` = VALUES(StockResult), `StockRateResult` = VALUES(StockRateResult), `DirectorResult` = VALUES(DirectorResult), `DirectorRateResult` = VALUES(DirectorRateResult)";
			if(!PMA_mysql_query($strSQL, $_Conn124)){
				$strMailCon .= $intIndex." Error :".PMA_mysql_error($_Conn124)."<br />\n";
				continue;
			}
			$intAffectRows += mysql_affected_rows($_Conn124);
		}
	}
	$strMailCon .= "PayupDate OK(".$intAffectRows.") <br />\n";
}
$strMailCon .= "End (".date("Y-m-d H:i:s").")<br />\n\n";

$arrMailTo[] = "eagle@uuss.net";
$arrMailTo[] = "sting@uuss.net";
$arrMailTo[] = "pinus@uuss.net";

$strMailSubject = "=?UTF-8?B?" . base64_encode("每日帳務拋轉記錄 (".date("Y-m-d").")") . "?=";
foreach($arrMailTo as $strMailTo){
	mail($strMailTo, $strMailSubject, $strMailCon, "From:Pinus<pinus@uuss.net>");
}

print $strMailCon;
?>